import request from '@/utils/request'
import type { ApiResponse, LoginParams, UserInfoResponse, PageParams, PageResult,UserInfo } from './types'

// 登录
export function login(data: LoginParams) {
  return request.post<ApiResponse<string>>('/auth/login', data)
}

// 获取用户信息
export function getUserInfo() {
  return request.get<ApiResponse<UserInfoResponse>>('/user/info')
}

// 登出
export function logout() {
  return request.post<ApiResponse>('/auth/logout')
}

// 获取用户列表
export function getUserList(params: PageParams & { username?: string; status?: string }) {
  return request.get<ApiResponse<PageResult<UserInfo>>>('/user/list', { params })
}

// 添加用户
export function addUser(data: Partial<UserInfo>) {
  return request.post<ApiResponse>('/user', data)
}

// 更新用户
export function updateUser(data: UserInfo) {
  return request.put<ApiResponse>(`/user/${data.id}`, data)
}

// 删除用户
export function deleteUser(id: number) {
  return request.delete<ApiResponse>(`/user/${id}`)
}

// 批量删除用户
export function batchDeleteUser(ids: number[]) {
  return request.delete<ApiResponse>('/user/batch', { data: { ids } })
}

// 获取用户角色
export function getUserRoles(userId: number) {
  return request.get<ApiResponse<number[]>>(`/user/${userId}/roles`)
}

// 分配用户角色
export function assignUserRoles(userId: number, roleIds: number[]) {
  return request.post<ApiResponse>(`/user/assign-roles`, { userId, roleIds })
}

// 修改密码
export function updatePassword(data: { oldPassword: string; newPassword: string }) {
  return request.put<ApiResponse>('/user/password', data)
}

// 重置密码
export function resetPassword(userId: number) {
  return request.put<ApiResponse>(`/user/${userId}/password/reset`)
} 